package com.home.admin.index;

import com.home.admin.config.WebClientConfig;
import com.home.common.annotation.NotResponseBody;
import com.home.common.constants.app.AppConstant;
import com.home.common.utils.string.StringUtils;
import com.home.security.browser.auth.entity.SysMenu;
import com.home.security.browser.auth.service.AuthUserService;
import com.home.security.browser.shiro.AdminWebSessionManager;
import com.home.security.browser.shiro.ShiroUtils;
import com.home.common.model.CurrentUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Description: WebAPP 模块后台进入 controller
 * Created by 王大宸 on 2020-09-28 20:43
 * Created with IntelliJ IDEA.
 */
@Controller
public class WebAppIndexController {
    private static final Logger logger = LoggerFactory.getLogger(WebAppIndexController.class);
    private static final String PATH_PREFIX = "home";
    @Autowired
    private AuthUserService authUserService;
    @Autowired
    private WebClientConfig webClientConfig;

    /***
     * 功能说明：跳转到后台管理界面
     *
     * @author 王大宸
     * @date 2020/9/28 20:45
     * @param
     * @return java.lang.String
     */
    @GetMapping(value = "/index")
    public String index(HttpServletRequest request,
                        HttpServletResponse response,
                        ModelMap modelMap) {
        response.addHeader(AdminWebSessionManager.LOGIN_TOKEN_KEY, ShiroUtils.getSessionId());
        CurrentUser userInfo = ShiroUtils.getUserInfo();
        modelMap.put("userName", userInfo.getName());
        String avatar = userInfo.getAvatar();
        if (StringUtils.isEmpty(avatar)) {
            avatar = "/static/home/images/avatar.jpg";
        }
        modelMap.put("avatar", avatar);

        modelMap.put("copyrightYear", webClientConfig.getCopyrightYear());
        modelMap.put("clientName", webClientConfig.getName());
        modelMap.put("version", webClientConfig.getVersion());
        modelMap.put("issuer", webClientConfig.getIssuer());
        return PATH_PREFIX + "/index";
    }

    /***
     * 功能说明：跳转到主页
     *
     * @author 王大宸
     * @date 2020/10/5 21:09
     * @param
     * @return java.lang.String
     */
    @GetMapping(value = "/console")
    public String console() {
        return PATH_PREFIX + "/console";
    }

    /***
     * 功能说明：登录成功之后,获取用户菜单
     *
     * @author 王大宸
     * @date 2020/10/7 16:28
     * @param
     * @return java.util.List<com.home.security.browser.auth.entity.SysMenu>
     */
    @GetMapping(value = "/user/menu")
    @ResponseBody
    @NotResponseBody
    public List<SysMenu> userHomeMenu() {
        CurrentUser userInfo = ShiroUtils.getUserInfo();
        return authUserService.getUserMenu(AppConstant.WEN_APP, userInfo);
    }

    /***
     * 功能说明：跳转到天气页面
     *
     * @author 王大宸
     * @date 2020/10/17 23:07
     * @param
     * @return java.lang.String
     */
    @RequestMapping(value = "/weather")
    public String weather() {
        return PATH_PREFIX + "/weather";
    }


}
